package com.sac.approval.controller;

import com.sac.approval.domain.ApprovalDO;
import com.sac.approval.domain.AttachmentDO;
import com.sac.approval.service.ApprovalQueryService;
import com.sac.approval.service.AttachmentService;
import com.sac.emp.domain.Employee;
import com.sac.emp.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 页面控制器
 * @author xiaodong
 * @date 2018/11/14 12:57
 * @desc 返回页面
 */
@Controller
@RequestMapping("/approval/view")
public class SACViewController {
	private String prefix="approval"  ;

	@Autowired
	private ApprovalQueryService approvalQueryService;

    @Autowired
    private EmpService empService;

    @Autowired
    private AttachmentService attachmentService;

	@GetMapping("/showAddComment/{title}")
	String showAddComment(Model model,@PathVariable String title) {
		model.addAttribute("title", title);
		return prefix + "/addComment";
	}

	@GetMapping("/showAddLine")
	String showAddLine(Model model) {
		return prefix + "/addLine";
	}

	@GetMapping("/showCreate")
	String showCreate(Model model) {
		return prefix + "/create";
	}

	@GetMapping("/showEditPostponed/{appTemplateId}")
	String showEditApproval(Model model,@PathVariable String appTemplateId) {
		model.addAttribute("appTemplateId", appTemplateId);
		return prefix + "/editPostponed";
	}

	@GetMapping("/showEditLineAndText/{appId}/{seq}")
	String showEditLineAndText(Model model,@PathVariable String appId,@PathVariable String seq) {
		model.addAttribute("appId", appId);
		model.addAttribute("seq", seq);
		return prefix + "/editLineAndText";
	}

	@GetMapping("/showPending")
	String showPending(Model model) {
		return prefix + "/pending";
	}
	
	@GetMapping("/showPendingDetail")
	String showPendingDetail(Model model,@RequestParam Map<String, Object> params) {
		ApprovalDO approval = approvalQueryService.getApproval(params.get("appId").toString());
		model.addAttribute("detail", approval);
		return prefix + "/pendingDetail";
	}
	
	@GetMapping("/showSubmission")
	String showSubmission(Model model) {
		return prefix + "/submission";
	}

	@GetMapping("/showPostponed")
	String showPostponed(Model model) {
		return prefix + "/postponed";
	}

	@GetMapping("/showPreliminary")
	String showPreliminary(Model model) {
		return prefix + "/preliminary";
	}

	@GetMapping("/showApproved")
	String showApproved(Model model) {
		return prefix + "/approved";
	}

	@GetMapping("/showNotification")
	String showNotification(Model model) {
		return prefix + "/notification";
	}

	@GetMapping("/showLineManagement")
	String showLineManagement(Model model) {
		return prefix + "/line";
	}

	@GetMapping("/showValidateUser/{appId}/{frameName}/{title}")
	String showValidateUser(Model model,@PathVariable String appId,@PathVariable String frameName,@PathVariable String title) {
		model.addAttribute("frameName", frameName);
		model.addAttribute("appId", appId);
		model.addAttribute("title", title);
		return prefix + "/validateUser";
	}

	@GetMapping("/showSetLocalPassword")
	String showSetLocalPassword(Model model) {
		return prefix + "/setLocalPassword";
	}

	@GetMapping("/showUserInfo")
	String showUserInfo(Model model) {
		return prefix + "/userInfo";
	}

	@GetMapping("/showPersonalSettings")
	String showPersonalSettings(Model model) {
		return prefix + "/personalSettings";
	}

	@GetMapping("/showTestLocale")
	String showTestLocale(Model model) {
		return prefix + "/testLocale";
	}

	@GetMapping("/showOptionSettings")
	String showOptionSettings(Model model) {
		return prefix + "/optionSettings";
	}

	@GetMapping("/showStatusInquiry/{appId}")
	String showStatusInquiry(Model model,@PathVariable String appId) {
		model.addAttribute("appId", appId);
		return prefix + "/statusInquiry";
	}

	@GetMapping("/showPrintApproval/{appId}")
	String showPrintApproval(Model model,@PathVariable String appId) {
		ApprovalDO approval = approvalQueryService.getApproval(appId);
		Employee emp = empService.getEmpDetailInfo(approval.getCreateUser());
        List<AttachmentDO> attachmentList = attachmentService.list(new HashMap<String,Object>(){{
            put("appId",appId);
        }});
        approval.setCreateUserNm(emp.getEmpNm());
        approval.setCreateUserJob(emp.getEmpJob());
        approval.setAttachmentList(attachmentList);
		model.addAttribute("approval", approval);
		return prefix + "/printInSamePage";
	}
}
